package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("Event9zxlController")
public class Event9zxlController extends CmiEmaController {

	@RequestMapping("/listEvent9zxls")
	public ModelAndView listEvent9zxls(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = event9zxlPaginationService.updatePagination(request, pagination);
		Set<Event9zxl> event9zxls = event9zxlPaginationService.getPaginationEvent9zxls(pagination);

		mav.addObject("event9zxls", event9zxls);
		mav.addObject("entityName", "event9zxl");
		mav.addObject("pagination", pagination);
		mav.setViewName("event9zxl/listEvent9zxls.jsp");
		return mav;
	}

	public String listEvent9zxls() {
		return "redirect:/listEvent9zxls";
	}

	@RequestMapping("/viewEvent9zxl")
	public ModelAndView viewEvent9zxl(@RequestParam Integer event9zxlId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("event9zxl", event9zxlDAO.findEvent9zxlByPrimaryKey(event9zxlId));
		mav.setViewName("event9zxl/viewEvent9zxl.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteEvent9zxls")
	public ModelAndView confirmDeleteEvent9zxls(
			@RequestParam(value = "deleteEvent9zxlsIds", required = true) Set<Integer> deleteEvent9zxlsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Event9zxl> deleteEvent9zxls = event9zxlDAO.findEvent9zxlsByIds(deleteEvent9zxlsIds);
		Set<Event9zxl> referencedEvent9zxls = event9zxlService.loadReferenceEvent9zxls(deleteEvent9zxls);
		boolean hasReferenced;
		if (referencedEvent9zxls.size() == 0) {
			mav.addObject("deleteEntitys", deleteEvent9zxls);
			hasReferenced = false;
		} else {
			deleteEvent9zxls.removeAll(referencedEvent9zxls);
			mav.addObject("referencedEntitys", referencedEvent9zxls);
			mav.addObject("deleteEntitys", deleteEvent9zxls);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Event9zxl");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listEvent9zxls");
		return mav;
	}

	@RequestMapping("/deleteEvent9zxl")
	public String deleteEvent9zxl(@RequestParam Integer event9zxlId) {
		Event9zxl event9zxl = event9zxlDAO.findEvent9zxlByPrimaryKey(event9zxlId);
		event9zxlService.deleteEvent9zxl(event9zxl);
		return "forward:/listEvent9zxls";
	}

	@RequestMapping("/confirmDeleteEvent9zxl")
	public ModelAndView confirmDeleteEvent9zxl(@RequestParam Integer event9zxlId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("event9zxl", event9zxlDAO.findEvent9zxlByPrimaryKey(event9zxlId));

		mav.setViewName("event9zxl/deleteEvent9zxl.jsp");

		return mav;
	}

	@RequestMapping("/deleteEvent9zxls")
	public String deleteEvent9zxls(
			@RequestParam(value = "deleteEvent9zxlsId", required = true) Set<Integer> deleteEvent9zxlsId) {
		event9zxlService.deleteEvent9zxls(deleteEvent9zxlsId);
		return "forward:/listEvent9zxls";
	}

	@RequestMapping("/editEvent9zxl")
	public ModelAndView editEvent9zxl(@RequestParam Integer event9zxlId) {

		ModelAndView mav = new ModelAndView();

		Event9zxl event9zxl = event9zxlDAO.findEvent9zxlByPrimaryKey(event9zxlId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("event9zxl", event9zxl);

		// 把所有的引用event9zxl对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(event9zxlService.loadEvent9zxlObjectReferencedSet(event9zxl));

		// 将编辑页面event9zxl添加到mav中
		mav.setViewName("event9zxl/editEvent9zxl.jsp");
		return mav;
	}

	@RequestMapping("/newEvent9zxl")
	public ModelAndView newEvent9zxl() {
		ModelAndView mav = new ModelAndView();
		Event9zxl event9zxl = new Event9zxl();

		mav.addObject("event9zxl", event9zxl);

		// 把所有的引用event9zxl对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(event9zxlService.loadEvent9zxlObjectReferencedSet(event9zxl));

		mav.addObject("newFlag", true);
		mav.setViewName("event9zxl/editEvent9zxl.jsp");

		return mav;
	}

	@RequestMapping("/saveEvent9zxl")
	public ModelAndView saveEvent9zxl(@ModelAttribute Event9zxl event9zxl) {

		ModelAndView mav = new ModelAndView();

		Event9zxl existingEvent9zxl = CmiHibernateProxyUtil
				.deproxy(event9zxlDAO.findEvent9zxlByPhysicalPrimaryKey(event9zxl.getNumber()));

		// 新建记录
		if (event9zxl.getId() == null) {
			if (event9zxl.equals(existingEvent9zxl)) {

				mav.addAllObjects(event9zxlService.loadEvent9zxlClassReferencedSet(event9zxl));
				// mav = editContinue(mav, event9zxl);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("event9zxl/editEvent9zxl.jsp");
			} else {
				event9zxlService.saveEvent9zxl(event9zxl);
				mav.setViewName("forward:/listEvent9zxls");
			}
		}
		// 编辑记录
		else {
			Event9zxl originEvent9zxl = CmiHibernateProxyUtil
					.deproxy(event9zxlDAO.findEvent9zxlByPrimaryKey(event9zxl.getId()));
			if (!event9zxl.equals(originEvent9zxl) && event9zxl.equals(existingEvent9zxl)) {
				mav.addAllObjects(event9zxlService.loadEvent9zxlClassReferencedSet(event9zxl));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("event9zxl/editEvent9zxl.jsp");
			} else {
				event9zxlService.saveEvent9zxl(event9zxl);
				mav.setViewName("forward:/listEvent9zxls");
			}
		}
		return mav;
	}

	@RequestMapping("/event9zxlController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
